package com.controller;

import java.io.IOException;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.entity.Tag;
import com.service.TagService;

@Controller @RequestMapping("/tag")
public class TagController {

	@Resource 
	TagService tagService;
	
	@RequestMapping(value="/add",method=RequestMethod.GET)
	public String gotoAddPage() {
		return "Tag/tag-add";
	}
	@RequestMapping(value="/add",method=RequestMethod.POST)
	public String add(ModelMap m,HttpServletRequest request) throws IOException {
		Tag tag=new Tag();
		tag.setTagName(request.getParameter("tagName"));
		tag.setTagDescription(request.getParameter("tagDescription"));
		tagService.addTag(tag);
		return "forward:/tag";   
	}
	@RequestMapping("")
	public String tag(ModelMap m) {
		List<Tag> tagList= tagService.listTag();
		m.put("tagList",tagList);
		return "Tag/tag-list";
	}
	
	@RequestMapping(value="/delete/{id}")
    public String deleteTag(@PathVariable("id") Integer id)  {
    	tagService.deleteTag(id);
        return "forward:/tag";
    }
}